﻿using Data.Service.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data.Service
{
    public class Intelligence_AutoResendData
    {
        public static SYTDataBaseEntities db = new SYTDataBaseEntities();

        //获取用户重发设置
        public static Intelligence_AutoResend  GetAutoResend(string memberId)
        {
            Intelligence_AutoResend autoResend = db.Intelligence_AutoResend.Where(M => M.MemberId == memberId).FirstOrDefault();
            
            if(autoResend != null)
            {
                return autoResend;
            }
            else
            {
                Intelligence_AutoResend newAutoResend = new Intelligence_AutoResend();
                newAutoResend.IsResend = false;
                newAutoResend.IsAllProduct = false;
                newAutoResend.IszidingyiProduct = false;
                newAutoResend.MemberId = memberId;
                newAutoResend.CreateTime = DateTime.Now;
                newAutoResend.UpdateTime = DateTime.Now;

                AddAutoResend(newAutoResend);//添加的数据库
                return newAutoResend;
            }
        }

        //修改数据
        public static bool UpdateAutoResend(Intelligence_AutoResend autoResend)
        {
            return BaseServer<Intelligence_AutoResend>.UpdateEntity(autoResend);
        }

        //添加一条数据
        public static bool AddAutoResend(Intelligence_AutoResend autoResend)
        {
            return BaseServer<Intelligence_AutoResend>.AddEntity(autoResend);
        }
        //获取当日和历史重发次数

        public static int GetSendCount(int mark,string memberId)
        {
            if (mark == 1)
            {
                return db.Intelligence_AutoAppraiseLog.Where(M => M.MemberId == memberId).Count();
            }
            else
            {
                DateTime crruntTime = DateTime.Now;
                return  db.Intelligence_AutoAppraiseLog.Where(M => M.MemberId == memberId && M.CreateTime < DateTime.Now && M.CreateTime > crruntTime.Date).Count();
            }
        }
    }
}
